package cn.itcast.bos.service.system.impl;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.system.MenuDao;
import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.MenuService;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {
	
	@Autowired
	private MenuDao menuDao;

	/**
	  * @Description: 查询所有菜单
	  * menuDao.findAll()  -- list长度33
	  * 查询顶级菜单  list长度4
	*/
	public List<Menu> findAll() {
		//查询顶级菜单
		return menuDao.findByParentMenuIsNull();
	}

	
	
	/**
	  * @Description: 如果不选择上级菜单，parentMenu对象瞬时态
	  * @return 
	*/
	public void save(Menu model) {
		Menu parentMenu = model.getParentMenu();
		if(parentMenu!=null&&parentMenu.getId()==null){
			model.setParentMenu(null);
		}
		menuDao.save(model);
	}




	//查询所有的菜单数据
	public List<Menu> findAllBySimple() {
		return menuDao.findAll();
	}



	/**
	  * @Description: 根据用户ID查询菜单记录
	  * @return 
	*/
	public List<Menu> showMenu() {
		//获取登陆用户
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		//内置管理员用户查询所有菜单
		if(user.getUsername().equals("admin")){
			return menuDao.findAll();
		} else {
			//其他用户动态查询
			return menuDao.findByUserId(user.getId());
		}
	}
	
	
}
